package com.example.exam10

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.example.exam10.dao.PersonDao
import com.example.exam10.entity.Person

@Database(version = 1, entities = [Person::class], exportSchema = false)
abstract class App : RoomDatabase() {
    abstract fun personDao(): PersonDao

    companion object {
        private var instance: App? = null

        @Synchronized
        fun getDatabase(context: Context): App {
            instance?.let {
                return it
            }
            return Room.databaseBuilder(context.applicationContext, App::class.java, "Database.db")
                    .allowMainThreadQueries()
                    .fallbackToDestructiveMigration()
                    .build()
                    .apply { instance = this }
        }
    }
}